﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HZU.ZP.Domain.IService;
using HZU.ZP.Domain.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HZU.ZP.Controllers
{
    public class PositionController : Controller
    {
        private IPositionService _positionService;
        private ICompanyService _companyService;
        private ICompayPictureService _compayPictureService;
        private ICompanyInformationService _companyInformationService;
        public PositionController(IPositionService positionService, ICompanyService companyService,
            ICompayPictureService compayPictureService, ICompanyInformationService companyInformationService)
        {
            _positionService = positionService;
            _companyService = companyService;
            _compayPictureService = compayPictureService;
            _companyInformationService = companyInformationService;
        }

        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }
        // GET: /<controller>/
        [Authorize(Roles = "Company")]
        [HttpPost]
        public IActionResult Add([FromForm]Position position)
        {
            var company = _companyService.GetId(position.Id.Value);
            if (position.Status == 1 && company.Status != 1)
            {
                return Json(AjaxResult.ReturnMessage(0, "发布失败，该企业未审核通过！"));
            }
            position.CompanyName = company.CompanyName;
            //position.ApplyNumber = 0;
            position.CreateTime = DateTime.Now;
            _positionService.Add(position);
            return Json(AjaxResult.ReturnMessage(1, "添加成功"));
        }

        [Authorize(Roles = "Company")]
        [HttpPost]
        public IActionResult Update(string email, int id, int status)
        {            
            Company company;
            Position position;
            if (status == 1)
            {
                company = _companyService.GetByEmail(email);
                if (company == null || company.Status != 1)
                    return Json(AjaxResult.ReturnMessage(0, "发布失败，该企业未审核通过！"));                
            }
            position = _positionService.GetById(id);
            if (position == null)
            {
                return Json(AjaxResult.ReturnMessage(0, "更新失败，未找到该职位消息！"));
            }
            else
            {
                position.Status = status;
                _positionService.Update(position);
                return Json(AjaxResult.ReturnMessage(1, "更新成功"));
            }
        }

        [Authorize(Roles ="Admin")]
        [HttpPost]
        public IActionResult UpdateAdmin(int id, int status)
        {          
            Position position;
            position = _positionService.GetById(id);
            if (position == null)
            {
                return Json(AjaxResult.ReturnMessage(0, "更新失败，未找到该职位消息！"));
            }
            else
            {
                position.Status = status;
                _positionService.Update(position);
                return Json(AjaxResult.ReturnMessage(1, "更新成功"));
            }
        }

        [Authorize(Roles ="Admin,Company")]
        [HttpPost]
        public IActionResult Delete(int id)
        {
            _positionService.Delete(id);
            return Json(AjaxResult.ReturnMessage(1, "删除成功"));
        }

        [Authorize(Roles = "Company")]
        [HttpGet]
        public IActionResult AddPosition(string email)
        {            
            Company company = _companyService.GetByEmail(email);
            if (company == null)
            {
                return NotFound();
            }
            else
            {
                ViewData["companyId"] = company.Id;

                ViewData["Title"] = "添加职位";
                return View();
            }
        }

        [Authorize]
        [HttpGet]
        // GET: /<controller>/
        public IActionResult GetComPosition(string email, int status)
        {
            string index = "position1";
            Company com;
            IList<Position> pos = new List<Position>();            
            if (status <= 0 || status >= 4)
            {
                status = 1;
            }
            com = _companyService.GetByEmail(email);
            if (com.Status == 2)
            {
                ViewData["Status"] = 999;
            }
            pos = _positionService.GetPositionList(com.Id.Value, status);
            if (status == 2)
            {
                index = "position2";
            }
            else if (status == 3)
            {
                index = "position3";
            }
            return View(index, pos);
        }

        [Authorize]
        [HttpGet]
        public IActionResult GetPosition(int id)
        {
            Position position;            
            position = _positionService.GetById(id);
            if (position == null)
                return NotFound();
            else
                return View(position);
        }

        [HttpGet]
        public IActionResult GetPositionList(int status, string name, string provincial, string municipal, string salary, string companySize, string companyType, int pageIndex = 1)
        {
            IList<Position> pos = new List<Position>();
            StringBuilder sql = new StringBuilder("select * from dbo.positions where status=1");
            
            if (!string.IsNullOrEmpty(name) && status == 1)
                sql.Append(" and name = '" + name + "'");
            else if (!string.IsNullOrEmpty(name))
                sql.Append(" and companyName = '" + name + "'");
            if (!"工作地点".Equals(provincial) && !string.IsNullOrEmpty(provincial))
                sql.Append(" and provincial = '" + provincial + "'" + " and municipal = '" + municipal + "'");
            if (!"薪资要求".Equals(salary) && !string.IsNullOrEmpty(salary))
                sql.Append(" and salary = '" + salary + "'");
            if ((!"公司规模".Equals(companySize) || !"公司类型".Equals(companyType)) && (!string.IsNullOrEmpty(companySize) || !string.IsNullOrEmpty(companyType)))
                sql.Append("and companyId in (select CompanyId from dbo.companyInformations where status = 1");
            if (!"公司规模".Equals(companySize) && !string.IsNullOrEmpty(companySize))
                sql.Append(" and companySize = '" + companySize + "'");
            if (!"公司类型".Equals(companyType) && !string.IsNullOrEmpty(companyType))
                sql.Append(" and companyType = '" + companyType + "'");
            if ((!"公司规模".Equals(companySize) || !"公司类型".Equals(companyType)) && (!string.IsNullOrEmpty(companySize) || !string.IsNullOrEmpty(companyType)))
                sql.Append(")");

            pos = _positionService.GetPositionList(sql.ToString());
            ViewData["Total"] = (int)Math.Ceiling((double)_positionService.GetPositionCount(sql.ToString()) / 20);
            ViewData["Status"] = status;
            ViewData["Name"] = name;
            ViewData["Salary"] = salary;
            ViewData["Provincial"] = provincial;
            ViewData["Municipal"] = municipal;
            ViewData["CompanySize"] = companySize;
            ViewData["CompanyType"] = companyType;                      
            if ((int)ViewData["Total"] == 0)
                ViewData["PageIndex"] = 0;
            else
                ViewData["PageIndex"] = pageIndex;

            ViewData["Title"] = "职位";
            return View(pos);
        }

        [HttpGet]
        public IActionResult GetPositionAndCompanyInfo(int id)
        {
            PositionView positionView = new PositionView();
            positionView.Position = _positionService.GetById(id);
            if (positionView.Position == null)
            {
                return NotFound();
            }
            positionView.CompanyInformation = _companyInformationService.Get(positionView.Position.CompanyId);
            positionView.CompayPictures = _compayPictureService.GetByCompanyId(positionView.Position.CompanyId);

            ViewData["Title"] = "职位信息";
            return View(positionView);
        }

        [Authorize]
        [HttpGet]
        public IActionResult CompanyPosition(int companyId)
        {
            int status = 1;
            IList<Position> positions = new List<Position>();
            positions = _positionService.GetPositionList(companyId, status);

            ViewData["Title"] = "企业职位";
            return View(positions);
        }
    }
}
